package com.ysten.basic.common.redis.scheduler;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.JedisPool;

import com.ysten.basic.common.redis.JedisTemplate;

/**
 * 任务管理，支持任务的安排与取消。
 */
public class JobManager
{
    
    private static Logger logger = LoggerFactory.getLogger(JobManager.class);
    
    private JedisTemplate jedisTemplate;
    
    private String sleepingJobKey;
    
    public JobManager(String jobName, JedisPool jedisPool)
    {
        jedisTemplate = new JedisTemplate(jedisPool);
        sleepingJobKey = Keys.getSleepingJobKey(jobName);
    }
    
    /**
     * 安排任务.
     */
    public void scheduleJob(final String job, final long delay, final TimeUnit timeUnit)
    {
        final long delayTimeMillis = System.currentTimeMillis() + timeUnit.toMillis(delay);
        jedisTemplate.zadd(sleepingJobKey, job, delayTimeMillis);
    }
    
    /**
     * 取消任务, 如果任务不存在或已被触发返回false, 否则返回true.
     */
    public boolean cancelJob(final String job)
    {
        boolean removed = jedisTemplate.zrem(sleepingJobKey, job);
        
        if (!removed)
        {
            logger.warn("Can't cancel job by value {}", job);
        }
        
        return removed;
    }
}
